package io.qar.categories.controller;

import io.qar.commons.log.annotation.LogOperation;
import io.qar.commons.mybatis.utils.Page;
import io.qar.commons.tools.config.SnowflakeConfig;
import io.qar.commons.tools.constant.Constant;
import io.qar.commons.tools.page.PageData;
import io.qar.commons.tools.utils.Result;
import io.qar.commons.tools.utils.ExcelUtils;
import io.qar.commons.tools.validator.AssertUtils;
import io.qar.commons.tools.validator.ValidatorUtils;
import io.qar.commons.tools.validator.group.AddGroup;
import io.qar.commons.tools.validator.group.DefaultGroup;
import io.qar.commons.tools.validator.group.UpdateGroup;
import io.qar.categories.dto.CategoriesDTO;
import io.qar.categories.excel.CategoriesExcel;
import io.qar.categories.service.CategoriesService;
import io.qar.config.TestConfig;
import io.qar.service.PageSerice;
import io.qar.utils.TreeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 分类信息表
*
* @author yyyyjinying 1020066395@qq.com
* @since 3.0 2024-06-02categories
*/
@RestController
@RequestMapping("categories")
@Api(tags="分类信息表")
//@RefreshScope
public class CategoriesController {
    @Autowired
    private PageSerice pageSerice;
    @Autowired
    private CategoriesService categoriesService;

//    @Value("${useLocalCache}")
//    private String useLocalCache;
    @Autowired
    private SnowflakeConfig snowflakeConfig;

    @GetMapping("useLocalCache")
    @ApiOperation("useLocalCache")
    public Result<Long> useLocalCache(){
        long l = snowflakeConfig.nextId();
        Long workerId = snowflakeConfig.getWorkerId();
        System.out.println("雪花算法："+l);
        return new Result<Long>().ok(workerId);
    }


    @GetMapping("list")
    @ApiOperation("列表")
    public Result<List<CategoriesDTO>> list(){
        List<CategoriesDTO> list = categoriesService.list(null);
        return new Result<List<CategoriesDTO>>().ok(list);
    }



    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    // @PreAuthorize("hasAuthority('sys:categories:page')")
    public Result<PageData<CategoriesDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CategoriesDTO> page = categoriesService.page(params);
        List<CategoriesDTO> list = page.getList();
        List<CategoriesDTO> build = TreeUtils.build(list);
        page.setList(build);
        return new Result<PageData<CategoriesDTO>>().ok(page);
    }

    @GetMapping("selectPage")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Page<CategoriesDTO>> selectPage(@ApiIgnore @RequestParam Map<String, Object> params){
        String name = params.get("name").toString();
        Page<CategoriesDTO> categoriesDTOPage = pageSerice.selectPageByName(name, Integer.parseInt(params.get(Constant.PAGE).toString()), Integer.parseInt(params.get(Constant.LIMIT).toString()));
        return new Result<Page<CategoriesDTO>>().ok(categoriesDTOPage);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    // @PreAuthorize("hasAuthority('sys:categories:info')")
    public Result<CategoriesDTO> get(@PathVariable("id") Long id){
        CategoriesDTO data = categoriesService.get(id);

        return new Result<CategoriesDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    // @PreAuthorize("hasAuthority('sys:categories:save')")
    public Result save(@RequestBody CategoriesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        categoriesService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    // @PreAuthorize("hasAuthority('sys:categories:update')")
    public Result update(@RequestBody CategoriesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        categoriesService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    // @PreAuthorize("hasAuthority('sys:categories:delete')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        categoriesService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    // @PreAuthorize("hasAuthority('sys:categories:export')")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CategoriesDTO> list = categoriesService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "分类信息表", list, CategoriesExcel.class);
    }

}